import type { IExam } from '@shared/types/exam';

import { httpRequest } from '../httpRequest';

export interface ExamListParams {
  page?: number
  pageSize?: number
  keyword?: string
  status?: string
  creator?: string
}

export interface ExamListResponse {
  data: IExam[]
  total_records: number
  page: number
  limit: number
  total_page: number
}

export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

export const examApi = {
  // 获取考试活动列表
  getExamList(params: ExamListParams): Promise<ApiResponse<ExamListResponse>> {
    return httpRequest.get('/api/v1/exam/list', { params })
  },

  // 获取单个考试活动详情
  getExamDetail(id: string): Promise<ApiResponse<IExam>> {
    return httpRequest.get(`/api/v1/exam/${id}`)
  },

  // 创建考试活动
  createExam(data: Partial<IExam>): Promise<ApiResponse<IExam>> {
    return httpRequest.post('/api/v1/exam/create', data)
  },

  // 更新考试活动
  updateExam(id: string, data: Partial<IExam>): Promise<ApiResponse<IExam>> {
    return httpRequest.put(`/api/v1/exam/${id}`, data)
  },

  // 删除考试活动
  deleteExam(id: string): Promise<ApiResponse<null>> {
    return httpRequest.delete(`/api/v1/exam/${id}`)
  },

  // 批量删除考试活动
  batchDeleteExams(ids: string[]): Promise<ApiResponse<{ deletedCount: number }>> {
    return httpRequest.delete(`/api/v1/exam/batch/${ids.join(',')}`)
  },

  // 更新考试活动状态
  updateExamStatus(id: string, isEnabled: boolean): Promise<ApiResponse<IExam>> {
    return httpRequest.patch(`/api/v1/exam/${id}/status`, { is_enabled: isEnabled })
  },
  // 获取考试排名
  getExamRanking(examId: string): Promise<ApiResponse<{
    rankings: Array<{
      user_id: string;
      username: string;
      score: number;
      total_score: number;
      rank: number;
      submit_time: string;
      duration?: number;
      record_id?: string;
    }>;
  }>> {
    return httpRequest.get(`/api/v1/exam/${examId}/ranking`)
  }
}

export default examApi